import os

from pyglet import image
from pyglet.media import Player
# our own files
from common import *

class GameSprite(object):
	
	def __init__(self, image_name, **kwargs):
		self.image = load_image(image_name)
		
		self.delete = False
		self.name = "sprite"
		
		self.x = 500
		self.y = 0
		self.health = 10
		self.dammage = 3
		self.speed = 100.00
		self.jump_force = 100
		self.vertical = 0
		self.gravity = 300
		self.right = False
		self.left = False
		self.killed_by = "none"
		# sound
		self.sound = Player()
		self.eos_action = Player.EOS_LOOP
		# time since last change in sprite animation
		self.animation_timer = 0
		# assigns the value of the keywords (if any) to the variables
		self.__dict__.update(kwargs)
		self.touch_ground()
	
	def update(self, dt):
		"""The basic logic calculations.
		Called once every frame."""
		
		# not used yet
		self.animation_timer += dt
		
		# is dead??
		if (self.health <= 0):
			self.die()
		
		# move?
		if self.right:
			self.x += limit(self.speed * dt, 9)
		if self.left:
			self.x -= limit(self.speed * dt, 9)
		
#		print (map.is_ground(self.x + self.image.width - 1, self.y + 11),
#			map.is_ground(self.x, self.y + 11),
#			map.is_ground(self.x + self.image.width - 1, self.y + self.image.height),
#			map.is_ground(self.x, self.y + self.image.height))
		
		# if colliding with ceiling bounce down
		if (map.is_ground(self.x + self.image.width - 8, self.y + self.image.height)
			or map.is_ground(self.x + 9, self.y + self.image.height)):
			self.y -= (self.y + self.image.height)%10 + 1
			if self.vertical > 0:
				self.vertical = -1
		
		# check collision with map
		if (map.is_ground(self.x + self.image.width - 1, self.y + 11)
			or map.is_ground(self.x + self.image.width - 1, self.y + self.image.height)):
			self.hit_wall(int((self.x + self.image.width - 1) / 10))
		
		elif (map.is_ground(self.x, self.y + 11)
			or map.is_ground(self.x, self.y + self.image.height)):
			self.hit_wall(int(self.x / 10))
		
		# gravity? only if sprite is above ground
		if self.touch_ground() and self.vertical <= 0:
			self.vertical = 0
		else:
			self.vertical -= self.gravity * dt
			self.y += limit(self.vertical * dt, 8)
			self.touch_ground()
	
	def touch_ground(self):
		"""Tests if sprite is on the ground.
		Moves the sprite up, if placed below
		ground level."""
		while (map.is_ground(self.x + self.image.width - 1, self.y)
			or map.is_ground(self.x, self.y)):
			self.y = (int)((self.y + 10) / 10) * 10
		return (map.is_ground(self.x + self.image.width - 1, self.y - 1)
			or map.is_ground(self.x, self.y - 1))
	
	def hit_wall(self, x):
		"""Called whenever sprite bumps against
		a wall"""
		# move to beginning of wall
		if (x == int(self.x / 10)):
			self.x = (int(self.x / 10) + 1) * 10
		else:
			self.x = (int(self.x / 10) - 1) * 10
		# switch direction
		self.right = not self.right
		self.left = not self.left
	
	def draw(self, offset):
		"""This function is called once every frame.
		Its purpose is to render the sprite"""
		self.image.blit(self.x - offset, self.y)
	
	def go_left(self, go):
		self.left = go
		if (self.left or self.right):
			self.play_sound()
		else:
			self.stop_sound()
	
	def go_right(self, go):
		self.right = go
		if (self.left or self.right):
			self.play_sound()
		else:
			self.stop_sound()
	
	def play_sound(self):
		self.sound.play()
	
	def stop_sound(self):
		self.sound.pause()
	
	def jump(self):
		if self.touch_ground():
			self.y += 1
			self.vertical += self.jump_force
	
	def force_jump(self):
		self.y += 1
		self.vertical += self.jump_force
	
	def die(self):
		#play die animation
		self.delete = True
	
	def intersect(self, other):
		"""Does the two sprites intersect?"""
		return not ((self.x > other.x + other.image.width)
			or (self.x + self.image.width < other.x)
			or (self.y + self.image.height < other.y)
			or (self.y > other.y + other.image.height))
	
	def contains(self, x, y):
		"""Does the sprite contains the point?"""
		return ((self.x < x) and (self.x + self.image.width > x)
			and (self.y < y) and (self.y + self.image.height > y))
	
	
